package com.ruoyi.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.PageParam;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.mapper.SysLogininforMapper;
import com.ruoyi.system.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor> implements ISysLogininforService {

    @Autowired
    private SysLogininforMapper logininforMapper;

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param pageParam  分页参数
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public Page<SysLogininfor> selectLogininforList(PageParam pageParam, SysLogininfor logininfor) {
        if (pageParam == null) {
            pageParam = new PageParam(0, -1);
        }

        return this.query()
                .like(StrUtil.isNotEmpty(logininfor.getIpaddr()), "ipaddr", logininfor.getIpaddr())
                .eq(StrUtil.isNotEmpty(logininfor.getStatus()), "status", logininfor.getStatus())
                .like(StrUtil.isNotEmpty(logininfor.getUserName()), "user_name", logininfor.getUserName())
                .ge(ObjectUtil.isNotEmpty(logininfor.getParams().get("beginTime")), "login_time", logininfor.getParams().get("beginTime"))
                .le(ObjectUtil.isNotEmpty(logininfor.getParams().get("endTime")), "login_time", logininfor.getParams().get("endTime"))
                .orderBy(StrUtil.isNotEmpty(pageParam.getOrderByColumn()), pageParam.asc(), StrUtil.toUnderlineCase(pageParam.getOrderByColumn()))
                .orderByDesc("info_id")
                .page(new Page<>(pageParam.getPageNum(), pageParam.getPageSize()));
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds) {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        logininforMapper.cleanLogininfor();
    }
}
